package com.zhou.plugin.packagejars.pack.impl;

import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.compiler.CompileContext;
import com.intellij.openapi.compiler.CompileStatusNotification;
import com.intellij.openapi.compiler.ex.CompilerPathsEx;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiPackage;
import com.zhou.plugin.packagejars.message.Messages;
import com.zhou.plugin.packagejars.pack.Packager;
import com.zhou.plugin.packagejars.util.Util;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Iterator;

/**
 * Created by Administrator on 2016/7/8.
 */

public class EachPacker implements Packager, CompileStatusNotification {
	private DataContext dataContext;
	private String exportPath;

	public EachPacker(DataContext dataContext, String exportPath) {
		this.dataContext = dataContext;
		this.exportPath = exportPath;
	}

	public void pack() {
		Project project = (Project) CommonDataKeys.PROJECT.getData(this.dataContext);
		Module module = (Module) LangDataKeys.MODULE.getData(this.dataContext);
		VirtualFile[] virtualFiles = (VirtualFile[]) CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(this.dataContext);
		String outPutPath = CompilerPathsEx.getModuleOutputPath(module, false);
		HashSet directories = new HashSet();
		VirtualFile[] jdkPath = virtualFiles;
		int i$ = virtualFiles.length;

		for (int directory = 0; directory < i$; ++directory) {
			VirtualFile psiDirectory = jdkPath[directory];
			Util.iterateDirectory(project, directories, psiDirectory);
		}

		String var18 = Util.getJDKPath(project);
		Iterator var19 = directories.iterator();

		while (true) {
			PsiDirectory var21;
			do {
				if (!var19.hasNext()) {
					return;
				}

				VirtualFile var20 = (VirtualFile) var19.next();
				var21 = PsiManager.getInstance(project).findDirectory(var20);
			} while (var21 == null);

			PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(var21);
			StringBuilder command = new StringBuilder(var18);
			command.append("jar");
			command.append(" cvf ");
			command.append(this.exportPath);
			command.append("/");
			command.append(psiPackage.getQualifiedName());
			command.append(".jar");
			File outPutDirectory = new File(outPutPath + "/" + psiPackage.getQualifiedName().replaceAll("\\.", "/"));
			if (outPutDirectory.exists()) {
				File[] process = outPutDirectory.listFiles();
				int e = process.length;

				for (File file : process) {
					if (file.isFile()) {
						command.append(" -C ");
						command.append(outPutPath);
						command.append(" ");
						command.append(psiPackage.getQualifiedName().replaceAll("\\.", "/"));
						command.append("/");
						command.append(file.getName());
					}
				}
			}

			Messages.info(project, command.toString());

			try {
				Process var22 = Runtime.getRuntime().exec(command.toString());
				BufferedReader var23 = new BufferedReader(new InputStreamReader(var22.getInputStream()));

				String var24;
				while ((var24 = var23.readLine()) != null) {
					Messages.info(project, var24);
				}
			} catch (Exception var17) {
				var17.printStackTrace();
			}
		}
	}

	public void finished(boolean b, int error, int i1, CompileContext compileContext) {
		if (error == 0) {
			this.pack();
		} else {
			Project project = (Project) CommonDataKeys.PROJECT.getData(this.dataContext);
			Messages.info(project, "compile error");
		}

	}
}
